package com.itic.ht.cms.exam.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.deser.Deserializers.Base;
import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.ht.cms.exam.persistence.model.Exam;
import com.itic.ht.cms.exam.persistence.model.ExamExample;
import com.itic.ht.cms.exam.persistence.model.ExamExample.Criteria;
import com.itic.ht.cms.exam.service.ExamService;
import com.itic.ht.cms.question.persistence.model.QuestionModel;
import com.itic.ht.cms.questype.persistence.model.QuestionType;
import com.itic.ht.cms.questype.service.QuestionTypeService;


@Controller
@RequestMapping(value = "/cms/exam")
public class ExamController extends BaseController {
	
	@Autowired
	private QuestionTypeService questionTypeService;
	@Autowired
	private ExamService examService;
	
	//////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
	private static final String MODEL_NAME_CHINA = "试卷";//中文名
	private static final String MODEL_NAME＿ENGLISH = "exam";//英文名
	private static final String PAGE_COMMON_PATH = "/cms/exam/exam";//通用路径
	private static final String PARENT_COMMON_URL = "/cms/questype/";//通用路径
	private static final String SUB_COMMON_URL = "/cms/exam/";//通用路径
	private static final String COMMON_URL = "/cms/exam/";//通用路径
	
	//////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
	
	/*
     * 主页面
     */
    @RequestMapping(value="/init")
    public String init(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
    	model.addAttribute("sub_common_url", SUB_COMMON_URL);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Main";
    }
    
    /*
     * 主页面
     */
    @RequestMapping(value="/initList")
    public String initList(String categoryId,Model model){
    	model.addAttribute("categoryId", categoryId);
    	QuestionType questionType = questionTypeService.get(categoryId);
    	model.addAttribute("categoryIdName", questionType.getName());
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
    	model.addAttribute("sub_common_url", SUB_COMMON_URL);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"List";
    }
    
    /*
     * 添加页面
     */
    @RequestMapping(value="/initAdd")
    public String initAdd(String categoryId,Model model){
    	if( StringHelper.isNotBlank(categoryId) ){
    		model.addAttribute("categoryId", categoryId);
    	}
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
    	model.addAttribute("sub_common_url", SUB_COMMON_URL);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Add";
    }
    
    /*
     * 编辑页面
     */
    @RequestMapping(value="/initEdit")
    public String initEdit(String id , String categoryId, Model model){
    	if( StringHelper.isNotBlank(id) ) {
    		Exam temp = examService.get(id);
    		model.addAttribute("editData",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
        	model.addAttribute("sub_common_url", SUB_COMMON_URL);
        	model.addAttribute("common_url", COMMON_URL);
        	model.addAttribute("categoryId", categoryId);
    	}else {
            throw new ParameterErrorException();
        }
    	return PAGE_COMMON_PATH+"Edit";
    }
    /**
     * 删除
     * 
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, String> del(String id) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String result = ResponseCode.SERVER_ERROR.getCode();
        if( StringHelper.isNotBlank(id) ){
     	   Exam temp = new Exam();
            temp.setId(id);
            result = examService.delete(temp);
        }
        
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
    
    @RequestMapping(value="/edit")
    @ResponseBody
    public Map<String,String> edit(Exam temp){
    
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         result = examService.edit(temp);
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    
    /**
     * 添加模板管理数据
     * @param android
     * @return
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public Map<String,String> add(Exam templet){
    	
    	    Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         
         //判断是添加新的商品还是  只添加一个SKU商品
         if(  StringHelper.isNotBlank(templet.getId()) ){
         }else{
         	String productId = IdGen.uuid();
         	Date date = new Date();
         	//产品的必填字段
         	templet.setId(productId);
         	templet.setCtime(date);
         	templet.setDelFlag(Integer.parseInt(DBConst.TRUE));
         	templet.setMtime(date);
         	templet.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper.get(Constants.USER_ID) : "unknown");
         	templet.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper.get(Constants.USER_ORG) : "unknown");
         	result = examService.add(templet);
         }
         resultMap.put(IticConst.RESULT, result);
         resultMap.put(IticConst.DATA, templet.getCategoryId());//将问题的类型返回前端页面
         return resultMap;
         
         //别忘记了添加分类的商品统计 
    }
    
    /**
     * 
     * @param goldType
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(Exam temp, HttpServletRequest request){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        
        ExamExample example = this.queryCondition(request,temp);
        // 封装分页参数
        example = this.handlePageQuery(request, example);
        
        
        // 获取信息总条数
        int total = examService.count(example);
        
        // 获取数据
        List<Exam> list = examService.list(example);
		if (list.size() > 0) {
			for (Exam temp2 : list) {
				temp2.setCategoryId(questionTypeService.getTypeName(temp2.getCategoryId()).getName());
			}
		}
        
        // 数据返回处理
        resultMap.put(IticConst.TOTAL_RECORDS, total);
        resultMap.put(IticConst.DISPLAY_RECORDS, total);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;

    }
    /**
     * <p>Title:queryCondition </p>
     * <p>Description:封装查询条件 </p>
     * @param jzdw
     * @param jzdwExample
     * @return
     */
    private ExamExample queryCondition(HttpServletRequest request,Exam temp) {
    	
    	   // 参数处理
    	ExamExample example = new ExamExample();
        // 按创建时间倒序
        example.setOrderByClause("ctime desc");
        example.setOrderByClause("reorder asc");
        	
        Criteria criteria = example.createCriteria().andDelFlagEqualTo(Integer.parseInt(DBConst.TRUE));
        //
        criteria.andCategoryIdEqualTo(temp.getCategoryId());
        // 查询条件
        if ( null != temp ) {
    	       //模糊查询
            if ( StringHelper.isNotBlank( temp.getExamName() ) ) {
                criteria.andExamNameLike("%" + temp.getExamName() + "%");
            }
            
        }
        return example;
    }

}
